package com.levi.queue;

import java.util.Scanner;

/**
 * @author shentong
 * @date 2021/8/22 18:03
 * @Description 数组实现的队列
 */
public class ArrayQueue {
    public static void main(String[] args) {
        QueueByArray queue = new QueueByArray(3);
        Scanner scanner = new Scanner(System.in);
        boolean flag = true;
        while (flag){
            System.out.println("a (往队列添加数据)");
            System.out.println("g (取数据)");
            System.out.println("h (查看队列头部的数据)");
            System.out.println("e (退出程序)");
            System.out.println("s (显示队列中的数据)");
            char c = scanner.next().charAt(0);
            switch (c){
                case 'a':
                    System.out.println("请输入要加入的数据:");
                    int i = scanner.nextInt();
                    try {
                        queue.add(i);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'g':
                    try {
                        System.out.println("取出的数据是:"+queue.get());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 's':
                    try {
                        queue.list();
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        System.out.println("头部数据是:"+queue.head());
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    flag = false;
                    scanner.close();
                    System.out.println("退出程序------");
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出----");
    }


}
class QueueByArray{
    private int front;
    private int real;
    private int maxSize;
    private int[] arr;

    public QueueByArray(int maxSize) {
        this.maxSize = maxSize;
        this.front = -1; //指向头部前一个数据的位置
        this.real = -1; //指向最后一个位置
        this.arr = new int[maxSize];
    }

    /**
     * 判断队列是否已满
     * @return
     */
    public boolean isFull() {
        if (real == maxSize - 1) {
            return true;
        }
        return false;
    }

    /**
     * 判断队列是否为空
     * @return
     */
    public boolean isEmpty() {
        if (real == front) {
            return true;
        }
        return false;
    }

    /**
     * 往队列里面加数据
     * @param a
     */
    public void add(int a) {
        //判断队列是否已满
        if (isFull()) {
            throw new RuntimeException("队列已满，无法添加数据");
        }
        //往队列里面添加数据
        arr[++real] = a;
    }

    /**
     * 取数据
     */
    public int get() {
        //判断队列是否为空
        if (isEmpty()) {
            throw new RuntimeException("队列为空，不能取数据");
        }
        return arr[++front];
    }

    /**
     * 查看队列的头部位置
     * @return
     */
    public int head() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空");
        }
        return arr[front + 1];
    }

    /**
     * 遍历队列里面的数据
     */
    public void list() {
        for (int i = 0; i < arr.length; i++) {
            System.out.printf("arr[%d]=%d\t",i,arr[i]);
        }
    }
}